<?
//here we define the functions used along this site

// we initialize this class
$tpl = new XTemplate("./template/tdriver.html");


// we define $PHP_SELF so it can be used inside templates
$tpl->assign("php_self",$PHP_SELF);

function login_admin($password){	
	global $ADMIN_PASSWORD;
	if(trim($password) == trim($ADMIN_PASSWORD)){
        $_SESSION['LOGIN_NAME'] = "admin";
        header("Location: index.php");
        exit();
	}
}
	
//logout for admin. in/out -nothing.
function logout_admin()
	{
   
	$_SESSION['LOGIN_NAME'] = "";
	}


function alternate_colors($color = "text_gray")
	{
	global $tpl;
	$color1="text_white";
	$color2="text_gray";
	
	$color == $color1 ? $color = $color2 : $color = $color1;
	$tpl->assign("color",$color);
	return($color);
	}

			

 function display_system_messages($id_message)
	{
		$query="select messages.message from messages,languages "
				."where messages.id_message='$id_message' and "
				."messages.id_language=languages.id and "
				."languages.language='".$_SESSION['LANGUAGE']."'";
		$result=mysql_query($query);
		if (1 <> mysql_num_rows($result)){
			return "$query";
		}
		else {
			$rez=mysql_fetch_array($result);
			return $rez['message'];
		}

	}

	
function check_title($name)
	{
	if ( strlen($name) == 0 )
	{
		return 0;
		exit;
	}
	else
	{
		$valid=preg_match("/[^A-z0-9\_.-]/",$name);
		$valid ? $valid=0 : $valid=1;
		return $valid;
	}
	}
			



function fai_imageresize_big_picture($filename,$output_file,$MAX_BIG_X,$JPEG_QUALITY,$sign=0)
	{
	;
		$result=0;
		//this one below you define it only when you want to add an extra sign
		if ( $sign <>  0 )
			{
			$EXTRA_SIGN=get_real_path()."/pics/sign.png";
			}
	if (is_file($filename)) {

		$image=imagecreatefromjpeg($filename);
  		$image_x = imagesx($image);
		$image_y = imagesy($image);

		
		$ratio = $image_y/$image_x;
		$MAX_BIG_Y=($MAX_BIG_X*$ratio);
		$new_x=$MAX_BIG_X;
		$new_y=$MAX_BIG_Y;
		
		/*if ( $image_x > $MAX_BIG_X && $image_y <= $MAX_BIG_Y )
			{
			$new_x=$MAX_BIG_X;
			$new_y=$image_y*($new_x/$image_x);
			}
		elseif ( $image_x <= $MAX_BIG_X && $image_y > $MAX_BIG_Y )
			{
			$new_y=$MAX_BIG_Y;
			$new_x=$image_x*($new_y/$image_y);
			}
		elseif ( $image_x > $MAX_BIG_X && $image_y > $MAX_BIG_Y )
			{
			if ( $image_x >= $image_y )
				{
				$new_x=$MAX_BIG_X;
				$new_y=$image_y*($new_x/$image_x);
				}
			else 	{
				$new_y=$MAX_BIG_Y;
                                $new_x=$image_x*($new_y/$image_y);
				}
			}
		else	{//we don't need any resizing
			$new_x=$image_x;
			$new_y=$image_y;
			}*/
	//we create the smaller(unchanged?) image
	
	$new_image=imagecreatetruecolor($new_x,$new_y);
	imagecopyresampled($new_image,$image,0,0,0,0,$new_x,$new_y,$image_x,$image_y);		




	//we save the resized image
	if ( @imagejpeg($new_image,$output_file,$JPEG_QUALITY) ) $result=1;
  	ImageDestroy($image);
	ImageDestroy($new_image);	

	chmod($output_file, 0777);  
	}
	return $result;
}//end fai_imageresize_big_picture		




function rotate_picture_left($old_picture,$new_picture,$direction)
	{
	
		if ($direction=="left")
		{
	  		$thumb=imagecreatefromjpeg($old_picture);
			$thumb_rotated=imagerotate($thumb,90,0);
			imagejpeg($thumb_rotated,$new_picture,100);			
			
		}
		else
		{
			$thumb=imagecreatefromjpeg($old_picture);
			$thumb_rotated=imagerotate($thumb,270,0);
			imagejpeg($thumb_rotated,$new_picture,100);	
		}
		
	}	





function move_record($id,$direction,$table_name,$table_field) { 
	if ( $direction == "asc" || $direction == "up" ) { 
		$query="SELECT $table_field FROM $table_name 
		WHERE $table_field < '$id'  
		ORDER BY $table_field DESC LIMIT 1"; 
	} 
	else { 
		$query="SELECT $table_field FROM $table_name 
		WHERE $table_field > '$id' 
		
		ORDER BY $table_field ASC LIMIT 1"; 
	} 
	$query_max_id=mysql_fetch_array(mysql_query("SELECT max($table_field) FROM $table_name")); 
	$greatest_id=$query_max_id[0]+1; 
	
	$query_replace=mysql_fetch_array(mysql_query($query)); 
	$replace_id=$query_replace[0]; 
	
	if ( $replace_id <> "" ) 
	{ 
		mysql_query("UPDATE $table_name SET $table_field=$greatest_id WHERE $table_field='$id'  "); 
		mysql_query("UPDATE $table_name SET $table_field=$id WHERE $table_field='$replace_id' "); 
		mysql_query("UPDATE $table_name SET $table_field=$replace_id WHERE $table_field='$greatest_id'  "); 
		
		$query1="update pictures_group set pictures=$greatest_id where pictures='$id' ";
		$query2="update pictures_group set pictures=$id where pictures=$replace_id ";
		$query3="update pictures_group set pictures=$replace_id where pictures='$greatest_id' ";
//		echo "mama are mere";
//		echo $query1."</br>";
//		echo $query2."</br>";
//		echo $query3."</br>";
		mysql_query($query1);
		mysql_query($query2);
		mysql_query($query3);
	}	

} 

function check_url ( $text ) { 
	$search =( "`\b(www\.[a-z\.\-0-9]+\.[a-z]{2,4}[\w/\.\-\?=~]*)\b/*`"); 
	preg_match($search,$text,$res); 
	if ($res[0] <> "") 
	{ 
		return 1; 
	} 
	else 
	{ 
		return 0; 
	} 

}

function addHttpString($string){
    $httpPos = strpos($string, 'http://');
    if($httpPos === false){
        $string = "http://".$string;
    }
    return $string;
}


function getGalleriesList($selected){
$q = "SELECT id, name
        FROM a_gallery
        ORDER BY name ASC";
$res = mysql_query($q);
$list = "<select name='gallery_id'>";
$list .= "<option value='-1'>None</option>";
while ($row = mysql_fetch_row($res)) {
        $isSelected = "";
        if($row[0] == $selected){            
            $isSelected = 'selected';
        }
        $list .= "<option value='{$row[0]}' $isSelected>{$row[1]}</option>";
}
$list .= "</select>";
return $list;
}
?>
